<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>Bit Generators &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../../about.html" >
    <link rel="index" title="Index" href="../../../genindex.html" >
    <link rel="search" title="Search" href="../../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../../index.html" >
    <link rel="up" title="Random sampling (numpy.random)" href="../index.html" >
    <link rel="next" title="numpy.random.BitGenerator" href="generated/numpy.random.BitGenerator.html" >
    <link rel="prev" title="numpy.random.zipf" href="../generated/numpy.random.zipf.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../../index.html">
      <img border=0 alt="NumPy" src="../../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../../routines.html" >Routines</a></li>
          <li class="active"><a href="../index.html" accesskey="U">Random sampling (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.random</span></code>)</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="generated/numpy.random.BitGenerator.html" title="numpy.random.BitGenerator"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="../generated/numpy.random.zipf.html" title="numpy.random.zipf"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Bit Generators</a><ul>
<li><a class="reference internal" href="#supported-bitgenerators">Supported BitGenerators</a><ul>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#seeding-and-entropy">Seeding and Entropy</a></li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../generated/numpy.random.zipf.html"
                        title="previous chapter">numpy.random.zipf</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="generated/numpy.random.BitGenerator.html"
                        title="next chapter">numpy.random.BitGenerator</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="bit-generators">
<h1>Bit Generators<a class="headerlink" href="#bit-generators" title="Permalink to this headline">¶</a></h1>
<p>The random values produced by <a class="reference internal" href="../generator.html#numpy.random.Generator" title="numpy.random.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Generator</span></code></a>
orignate in a BitGenerator.  The BitGenerators do not directly provide
random numbers and only contains methods used for seeding, getting or
setting the state, jumping or advancing the state, and for accessing
low-level wrappers for consumption by code that can efficiently
access the functions provided, e.g., <a class="reference external" href="https://numba.pydata.org">numba</a>.</p>
<div class="section" id="supported-bitgenerators">
<h2>Supported BitGenerators<a class="headerlink" href="#supported-bitgenerators" title="Permalink to this headline">¶</a></h2>
<p>The included BitGenerators are:</p>
<ul class="simple">
<li><p>PCG-64 - The default. A fast generator that supports many parallel streams
and can be advanced by an arbitrary amount. See the documentation for
<a class="reference internal" href="generated/numpy.random.PCG64.advance.html#numpy.random.PCG64.advance" title="numpy.random.PCG64.advance"><code class="xref py py-meth docutils literal notranslate"><span class="pre">advance</span></code></a>. PCG-64 has a period of <img class="math" src="../../../_images/math/91018ad7005c40f683c645b99dd28ae741d2de00.svg" alt="2^{128}"/>. See the <a class="reference external" href="http://www.pcg-random.org/">PCG
author’s page</a> for more details about this class of PRNG.</p></li>
<li><p>MT19937 - The standard Python BitGenerator. Adds a <a class="reference internal" href="generated/numpy.random.MT19937.jumped.html#numpy.random.MT19937.jumped" title="numpy.random.MT19937.jumped"><code class="xref py py-obj docutils literal notranslate"><span class="pre">MT19937.jumped</span></code></a>
function that returns a new generator with state as-if <img class="math" src="../../../_images/math/91018ad7005c40f683c645b99dd28ae741d2de00.svg" alt="2^{128}"/> draws have
been made.</p></li>
<li><p>Philox - A counter-based generator capable of being advanced an
arbitrary number of steps or generating independent streams. See the
<a class="reference external" href="https://www.deshawresearch.com/resources_random123.html">Random123</a> page for more details about this class of bit generators.</p></li>
<li><p>SFC64 - A fast generator based on random invertible mappings. Usually the
fastest generator of the four. See the <a class="reference external" href="http://pracrand.sourceforge.net/RNG_engines.txt">SFC author’s page</a> for (a little)
more detail.</p></li>
</ul>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.random.BitGenerator.html#numpy.random.BitGenerator" title="numpy.random.BitGenerator"><code class="xref py py-obj docutils literal notranslate"><span class="pre">BitGenerator</span></code></a>([seed])</p></td>
<td><p>Base Class for generic BitGenerators, which provide a stream of random bits based on different algorithms.</p></td>
</tr>
</tbody>
</table>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="mt19937.html">MT19937</a></li>
<li class="toctree-l1"><a class="reference internal" href="pcg64.html">PCG64</a></li>
<li class="toctree-l1"><a class="reference internal" href="philox.html">Philox</a></li>
<li class="toctree-l1"><a class="reference internal" href="sfc64.html">SFC64</a></li>
</ul>
</div>
</div>
</div>
<div class="section" id="seeding-and-entropy">
<h1>Seeding and Entropy<a class="headerlink" href="#seeding-and-entropy" title="Permalink to this headline">¶</a></h1>
<p>A BitGenerator provides a stream of random values. In order to generate
reproducible streams, BitGenerators support setting their initial state via a
seed. All of the provided BitGenerators will take an arbitrary-sized
non-negative integer, or a list of such integers, as a seed. BitGenerators
need to take those inputs and process them into a high-quality internal state
for the BitGenerator. All of the BitGenerators in numpy delegate that task to
<a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a>, which uses hashing techniques to ensure that even low-quality
seeds generate high-quality initial states.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">numpy.random</span> <span class="kn">import</span> <span class="n">PCG64</span>

<span class="n">bg</span> <span class="o">=</span> <span class="n">PCG64</span><span class="p">(</span><span class="mi">12345678903141592653589793</span><span class="p">)</span>
</pre></div>
</div>
<p><a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> is designed to be convenient for implementing best practices.
We recommend that a stochastic program defaults to using entropy from the OS so
that each run is different. The program should print out or log that entropy.
In order to reproduce a past value, the program should allow the user to
provide that value through some mechanism, a command-line argument is common,
so that the user can then re-enter that entropy to reproduce the result.
<a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> can take care of everything except for communicating with the
user, which is up to you.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">numpy.random</span> <span class="kn">import</span> <span class="n">PCG64</span><span class="p">,</span> <span class="n">SeedSequence</span>

<span class="c1"># Get the user&#39;s seed somehow, maybe through `argparse`.</span>
<span class="c1"># If the user did not provide a seed, it should return `None`.</span>
<span class="n">seed</span> <span class="o">=</span> <span class="n">get_user_seed</span><span class="p">()</span>
<span class="n">ss</span> <span class="o">=</span> <span class="n">SeedSequence</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;seed = </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ss</span><span class="o">.</span><span class="n">entropy</span><span class="p">))</span>
<span class="n">bg</span> <span class="o">=</span> <span class="n">PCG64</span><span class="p">(</span><span class="n">ss</span><span class="p">)</span>
</pre></div>
</div>
<p>We default to using a 128-bit integer using entropy gathered from the OS. This
is a good amount of entropy to initialize all of the generators that we have in
numpy. We do not recommend using small seeds below 32 bits for general use.
Using just a small set of seeds to instantiate larger state spaces means that
there are some initial states that are impossible to reach. This creates some
biases if everyone uses such values.</p>
<p>There will not be anything <em>wrong</em> with the results, per se; even a seed of
0 is perfectly fine thanks to the processing that <a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> does. If you
just need <em>some</em> fixed value for unit tests or debugging, feel free to use
whatever seed you like. But if you want to make inferences from the results or
publish them, drawing from a larger set of seeds is good practice.</p>
<p>If you need to generate a good seed “offline”, then <code class="docutils literal notranslate"><span class="pre">SeedSequence().entropy</span></code>
or using <code class="docutils literal notranslate"><span class="pre">secrets.randbits(128)</span></code> from the standard library are both
convenient ways.</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a>([entropy, spawn_key, pool_size])</p></td>
<td><p>SeedSequence mixes sources of entropy in a reproducible way to set the initial state for independent and very probably non-overlapping BitGenerators.</p></td>
</tr>
</tbody>
</table>
</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>